package mac5789.graph;

import java.util.Arrays;

/**
 * Classe abstrata base para implementações de grafos
 * 
 * @author lundberg
 * 
 */
public abstract class AbstractGraph implements Graph {

    private final boolean[][] edges;

    /**
     * Construtor
     * 
     * @param nVertexes
     *            nVertexes
     */
    public AbstractGraph(int nVertexes) {
        this.edges = new boolean[nVertexes][nVertexes];
        this.clear();
    }

    /**
     * Limpa as arestas
     */
    protected void clear() {
        for (int i = 0; i < this.edges.length; i++) {
            Arrays.fill(this.edges[i], false);
        }
    }

    /**
     * {@inheritDoc}
     */
    public int getNVertexes() {
        return this.edges.length;
    }

    /**
     * {@inheritDoc}
     */
    public boolean areNeighbours(int aVertex, int otherVertex) {
        return this.edges[aVertex][otherVertex];
    }

    /**
     * Adiciona uma aresta a um grafo.
     * 
     * @param aVertex
     *            aVertex
     * @param otherVertex
     *            otherVertex
     * @return self
     */
    protected AbstractGraph addEdge(int aVertex, int otherVertex) {
        this.edges[aVertex][otherVertex] = true;
        this.edges[otherVertex][aVertex] = true;
        return this;
    }
}